//
//  ViewController.m
//  Travels
//
//  Created by 郭明皓 on 15/10/21.
//  Copyright (c) 2015年 郭明皓工作室. All rights reserved.
//

#import "ViewController.h"
#import "GMHActivity.h"
#import "GMHAcitvityTableViewCell.h"
#import "GMHblockOneTableViewCell.h"
#import "GMHSecondViewController.h"
#import "GMHScrollerImages.h"
#import "GMHActivityCollectionViewCell.h"
#import "GMHcollectionImage.h"
#import "GMHButtonViewController.h"
#import "FirstCollectView.h"
#import "GMCitys.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import "GMHDataFiled.h"
#import "SearchController.h"
#import <Reachability.h>
#import "MBProgressHUD.h"
#import "DKNightVersion.h"
#import "GMH_Share.h"
#import "AppDelegate.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate,UIScrollViewDelegate, UISearchBarDelegate>
@property (nonatomic, retain)UITableView *tableview;
@property (nonatomic, retain)UITableView *BlockTableview;
@property (nonatomic, retain)UIView *headerView;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UIScrollView *scrollerView;
@property (nonatomic, retain)UIPageControl *page;
@property (nonatomic, retain)NSMutableArray *activityArr;
@property (nonatomic, retain)NSMutableArray *imagesArr;
@property (nonatomic, retain)NSMutableArray *blockOnearr;
@property (nonatomic, retain)NSMutableArray *blockTwoarr;
@property (nonatomic, retain)NSMutableArray *signArr;
@property (nonatomic, retain)NSMutableArray *collectionImageArr;
@property (nonatomic, assign)NSInteger flag;
@property (nonatomic, assign) int pageIndex;
@property (nonatomic, assign) BOOL isUpLoading;
@property (nonatomic, assign)NSMutableArray *refershArr;
@property (nonatomic, assign)NSInteger pages;
@property (nonatomic, copy) NSString *strURL;
@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, retain)UIButton *buttons;
@property (nonatomic, retain)UISearchBar *search;
@property (nonatomic, retain)UIView *blockone;
@property (nonatomic, retain)Reachability *hostReach;
@end

@implementation ViewController
- (void)dealloc
{
    [_blockone release];
    [_search release];
    [_buttons release];
    [_collectionImageArr release];
    [_signArr release];
    [_page release];
    [_blockTwoarr release];
    [_blockOnearr release];
    [_BlockTableview release];
    [_headerView  release];
    [_scrollerView release];
    [_imagesArr release];
    [_activityArr release];
    [_collectionView release];
    [_tableview release];
    [_strURL release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
     self.tabBarController.tabBar.hidden = NO;
    
    if ([GMH_Share SingleShare].isNight == YES) {
        self.collectionView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
        _blockone.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    }
    else
    {
        self.collectionView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
        _blockone.backgroundColor = [UIColor whiteColor];
    }
    
    if ([self isConnectionAvailable]) {
        [self viewDidLoad];
        
    }
    else
    {
        NSLog(@"没有网络时");
        //1.找沙盒路径
        NSString *sandboxpath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        //2.拼接电影的文件夹
        NSString *movieFile = [sandboxpath stringByAppendingPathComponent:@"Travel"];
        //3.创建一个文件管理者
        NSFileManager *manager = [NSFileManager defaultManager];
        //通过管理者调用方法,根据路径判断文件夹是否存在
        if (![manager fileExistsAtPath:movieFile]) {
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];//<span style="font-family: Arial, Helvetica, sans-serif;">MBProgressHUD为第三方库，不需要可以省略或使用AlertView</span>
            hud.removeFromSuperViewOnHide =YES;
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"当前网络不可用，请检查网络连接";  //提示的内容
            hud.minSize = CGSizeMake(132.f, 58.0f);
            [hud hide:YES afterDelay:3];
            
        }
        else
        {
            NSString *sendBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
            NSString *CachesPath = [sendBoxPath stringByAppendingPathComponent:@"Travel/Activity/Title.plist"];
            NSDictionary *tabledic = [NSKeyedUnarchiver unarchiveObjectWithFile:CachesPath];
            self.activityArr = [GMHActivity baseModelArry:tabledic[@"data"]];
            
            
            //轮播图
            NSString *sendBoxPaths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
            NSString *CachesPaths = [sendBoxPaths stringByAppendingPathComponent:@"Travel/Activity/lunbotu.plist"];
            NSDictionary *scrollDic = [NSKeyedUnarchiver unarchiveObjectWithFile:CachesPaths];
            self.imagesArr = [GMHScrollerImages baseModelArry:scrollDic[@"data"][@"slide"]];
            self.blockOnearr = [GMHScrollerImages baseModelArry:scrollDic[@"data"][@"subject"]];
            
            //        collection
            NSString *collectionBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
            NSString *collectionPath = [collectionBoxPath stringByAppendingPathComponent:@"Travel/Activity/Collect.plist"];
            NSDictionary *collectDic = [NSKeyedUnarchiver unarchiveObjectWithFile:collectionPath];
            self.collectionImageArr = [GMHcollectionImage baseModelArry:collectDic[@"content"][1][@"ad"]];
            
            [self.tableview reloadData];
            [self.collectionView reloadData];
        }
    }
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(clip) userInfo:nil repeats:YES];
//    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(select) userInfo:nil repeats:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.nightBarTintColor = UIColorFromRGB(0x444444);
    self.view.nightBackgroundColor = [UIColor blackColor];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(click)];
    
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
    
    CGRect rect = CGRectMake(0, 64,
                             self.view.frame.size.width,
                             self.view.frame.size.height - 64);
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor =[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
    //tableView创建
    self.tableview = [[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    [_tableview release];
    self.tableview.delegate = self;
    self.tableview.dataSource =self;;
    self.tableview.rowHeight = 100;
    
    self.tableview.nightBackgroundColor = [UIColor blackColor];
    self.tableview.nightSeparatorColor = [UIColor darkGrayColor];

    [self downPull];
    [self upPull];
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 550)];
    self.tableview.tableHeaderView = self.headerView;
    [_headerView release];
    
    
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize = CGSizeMake(self.view.frame.size.width / 6, 80);
    flow.minimumLineSpacing = 0;//行间距
    flow.minimumInteritemSpacing = 0;//列间距
    flow.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    flow.headerReferenceSize = CGSizeMake(0, 150);//创建顶部区域
    flow.footerReferenceSize = CGSizeMake(0, 300);//创建底部区域
    
    //创建collectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 550) collectionViewLayout:flow];
    [self.headerView addSubview:self.collectionView];
    [self.collectionView release];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[GMHActivityCollectionViewCell class] forCellWithReuseIdentifier:@"collectionView"];
    self.collectionView.scrollEnabled = NO;
    //注册头部区域
    [self.collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.collectionView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    self.activityArr = [NSMutableArray array];
    
}

- (void)click
{
    SearchController *city = [[SearchController alloc]init];
    [self.navigationController pushViewController:city animated:YES];
    [city release];
    
}
- (void)downPull
{
    __block ViewController *find = self;
    
    self.tableview.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        self.isUpLoading = NO;//标记为下拉操作
        [find createData];
    }];
    [self.tableview.header beginRefreshing];
}

- (void)upPull
{
    __block ViewController *find = self;
    self.tableview.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self.tableview.footer endRefreshing];
        self.pageIndex++;
        self.isUpLoading = YES;//标记为上拉操作
        [find createData];
    }];
    
}

//- (void)select
//{
//    //开启网络状况的监听
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChangeds:) name:kReachabilityChangedNotification object:nil];
//    
//    self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
//    //开始监听，会启动一个run loop
//    [self.hostReach startNotifier];
//}
#pragma mark 数据加载
- (void)createData{
    
    
    [GMHDataFiled isHaveFileDocument];
    [GMHDataFiled isHaveFileActivity];

    //tableview列表信息
    NSString *strurl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/recommands/trip?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=A0000055A7CDF8&track_app_version=6.8.1&track_app_channel=hicloud&track_device_info=hwC8818&track_os=Android4.4.4&app_installtime=1445324313193&lat=38.88354&lon=121.544481&type=index&page=%d&count=10", _pageIndex];
    if (_isUpLoading == NO) {//说明是下拉,就要清空数据
        [self.activityArr removeAllObjects];
    }
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSMutableArray *array = [GMHActivity baseModelArry:dic[@"data"]];
        [self.activityArr addObjectsFromArray:array];
            if (_activityArr.count > 0 ) {
            [self.tableview.header endRefreshing];
            [self.tableview.footer endRefreshing];
            [self.tableview reloadData];
        }
        
       
        NSString *sendBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSString *CachesPath = [sendBoxPath stringByAppendingPathComponent:@"Travel/Activity/Title.plist"];
        NSLog(@"%@", CachesPath);

        [NSKeyedArchiver archiveRootObject:dic toFile:CachesPath];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
        
    }];
    //轮播图网址
    [manager GET:@"http://open.qyer.com/qyer/recommands/entry?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=99000505125188&track_app_version=6.8.1&track_app_channel=xiaomi&track_device_info=aries&track_os=Android5.0.2&app_installtime=1445388019292&lat=38.883567&lon=121.544437&channel=xiaomi" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.imagesArr = [GMHScrollerImages baseModelArry:dic[@"data"][@"slide"]];
        self.blockOnearr = [GMHScrollerImages baseModelArry:dic[@"data"][@"subject"]];
        self.blockTwoarr = [GMHScrollerImages baseModelArry:dic[@"data"][@"discount"]];
        [self.collectionView reloadData];

//        [GMHDataFiled isHaveFileDocument];
        NSString *sendBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSString *CachesPath = [sendBoxPath stringByAppendingPathComponent:@"Travel/Activity/lunbotu.plist"];
        [NSKeyedArchiver archiveRootObject:dic toFile:CachesPath];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"轮播图");
    }];
    //collectionView网址

        [manager GET:@"http://apiphp.yaochufa.com/you/advertiselist/AdList?system=android&area_code=210200&column=bannerRound%2CbannerScroll%2CbannerSquare&channel=xiaomimarket&imei=ffffffff-9317-02f2-ffff-fffff60c2478&regId=060dc37cc2d&version=4.3.1&deviceToken=ffffffff-9317-02f2-ffff-fffff60c2478" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"成功");
            NSDictionary *dic = responseObject;
            self.collectionImageArr = [GMHcollectionImage baseModelArry:dic[@"content"][1][@"ad"]];
            
            [self.collectionView reloadData];
            
            
            NSString *sendBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
            NSString *CachesPath = [sendBoxPath stringByAppendingPathComponent:@"Travel/Activity/Collect.plist"];
            [NSKeyedArchiver archiveRootObject:dic toFile:CachesPath];
            NSLog(@"%@", CachesPath);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@", error);
        }];
        [self.collectionView reloadData];
}

#pragma mark tableview协议方法
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
        return @"看热门游记";
}
//控制section的高度值
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([GMH_Share SingleShare].isNight == YES) {
        tableView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    }
//    [_activityArr count]?[[[[_activityArr objectAtIndex:section] objectForKey:@"subs"] objectAtIndex:0] objectForKey:@"section"]:@"";
//    
//    [_activityArr count]?[[[_activityArr objectAtIndex:section] objectForKey:@"subs"] count]:0;
    
        return self.activityArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
        static NSString *strs = @"tableView";
        GMHAcitvityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
        if (!cell) {
            cell = [[GMHAcitvityTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strs];
        }
        
    if (self.activityArr.count != 0) {
        GMHActivity *gmh = self.activityArr[indexPath.row];
        cell.title.text = gmh.title;
        [cell.leftimageView sd_setImageWithURL:[NSURL URLWithString:gmh.photo]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
        cell.username.text = gmh.username;
        cell.replysImage.image = [UIImage imageNamed:@"replys.png"];
        cell.replys.text = gmh.replys;
        cell.viewImage.image = [UIImage imageNamed:@"eyes.png"];
        cell.views.text = [NSString stringWithFormat:@"%@", gmh.views];
        return cell;

    }
    else
    {
        return nil;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.tabBarController.tabBar.hidden = YES;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GMHSecondViewController *sec = [[GMHSecondViewController alloc]init];
    [self.navigationController pushViewController:sec animated:YES];
    [sec release];
    
    GMHActivity *gmh = self.activityArr[indexPath.row];

    sec.url = gmh.view_url;
}



//collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GMHActivityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView" forIndexPath:indexPath];
//    cell.contentView.backgroundColor = [UIColor cyanColor];
    
    GMHcollectionImage *image = self.collectionImageArr[indexPath.row];
    NSDictionary *dc = image.ct;

    [cell.images sd_setImageWithURL:[NSURL URLWithString:dc[@"app_picpath"]]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
    cell.label.text = dc[@"title"];
    
    return cell;
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.tabBarController.tabBar.hidden = YES;
    FirstCollectView *collection = [[FirstCollectView alloc]init];
    [self.navigationController pushViewController:collection animated:YES];
    [collection release];
    
    GMHcollectionImage *image = self.collectionImageArr[indexPath.row];
    collection.tag = image.ct[@"app_url"];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark collectionview视图headerview和footerview
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        
        self.scrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
        [headerView addSubview:self.scrollerView];
        [self.scrollerView release];
        self.scrollerView.contentSize = CGSizeMake((self.imagesArr.count + 2)* WIDTH, 0);
        self.scrollerView.pagingEnabled = YES;
        self.scrollerView.delegate = self;
        self.scrollerView.showsHorizontalScrollIndicator = NO;
        self.scrollerView.contentOffset = CGPointMake(WIDTH, 0);
        
                UIImageView *firstImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
                [self.scrollerView addSubview:firstImage];
                [firstImage release];
                [firstImage sd_setImageWithURL:[NSURL URLWithString:[self.imagesArr[self.imagesArr.count - 1] photo]]];
                firstImage.userInteractionEnabled = YES;

                //最后一张图片
                UIImageView *lastImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.imagesArr.count + 1) * WIDTH, 0, WIDTH, 150)];
                [self.scrollerView addSubview:lastImage];
                [lastImage release];
                [lastImage sd_setImageWithURL:[NSURL URLWithString:[self.imagesArr[0] photo]]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
                lastImage.userInteractionEnabled = YES;

        //轮播图
        for (NSInteger i = 0 ; i < self.imagesArr.count; i++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((i + 1) * WIDTH, 0, WIDTH, 150)];
            GMHScrollerImages *images = self.imagesArr[i];
            [imageView sd_setImageWithURL:[NSURL URLWithString:images.photo]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
            [self.scrollerView addSubview:imageView];
            [imageView release];
            imageView.userInteractionEnabled = YES;
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(taps:)];
            [imageView addGestureRecognizer:tap];
            [tap release];
        }

        self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.center.x - WIDTH / 4, 130, WIDTH / 2, 20)];
        [headerView addSubview:self.page];
        [self.page release];
        self.page.numberOfPages = self.imagesArr.count;
        

        return headerView;
    }
    else
    {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        
        _blockone = [[UIView alloc]initWithFrame:CGRectMake(5, 15, WIDTH - 10, 290)];
        [footerView addSubview:_blockone];
        [_blockone release];
//        footerView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
//        footerView.nightBackgroundColor = [UIColor blackColor];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH - 10, 20)];
        [_blockone addSubview:label];
        [label release];
        label.text = @"发现下一站";
        label.textColor = [UIColor darkGrayColor];
        
        GMHScrollerImages *imageone = self.blockOnearr[0];
        UIImageView *imageviewone = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, WIDTH - 10, 120)];
        [_blockone addSubview:imageviewone];
        [imageviewone release];
        [imageviewone sd_setImageWithURL:[NSURL URLWithString:imageone.photo]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
        imageviewone.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap1:)];
        [imageviewone addGestureRecognizer:tap1];
        [tap1 release];

        
        GMHScrollerImages *imagetwo = self.blockOnearr[1];
        UIImageView *imageViewtwo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 150, WIDTH / 2 - 10, 100)];
        [_blockone addSubview:imageViewtwo];
        [imageViewtwo release];
        [imageViewtwo sd_setImageWithURL:[NSURL URLWithString:imagetwo.photo]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
        imageViewtwo.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap2:)];
        [imageViewtwo addGestureRecognizer:tap2];
        [tap2 release];

        
        GMHScrollerImages *imagethree = self.blockOnearr[2];
        UIImageView *imageviewThree = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH / 2, 150, WIDTH / 2 - 10, 100)];
        [_blockone addSubview:imageviewThree];
        [imageviewThree release];
        [imageviewThree sd_setImageWithURL:[NSURL URLWithString:imagethree.photo]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
        imageviewThree.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap3:)];
        [imageviewThree addGestureRecognizer:tap3];
        [tap3 release];

        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(0, 250, WIDTH - 10, 35);
        [button setTitle:@"查看全部 ➤" forState:UIControlStateNormal];
        [_blockone addSubview:button];
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
        button.nightTitleColor = [UIColor darkGrayColor];
        
        
        return footerView;
    }
    return nil;
}
#pragma mark 全部按钮点击
- (void)click:(UIButton *)button
{
    self.tabBarController.tabBar.hidden = YES;

    GMHButtonViewController *detail = [[GMHButtonViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}
- (void)tap1:(UITapGestureRecognizer *)tap
{
    self.tabBarController.tabBar.hidden = YES;
    GMHSecondViewController *detail = [[GMHSecondViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];

    GMHActivity *act = self.blockOnearr[0];
    detail.url = act.url;

}
- (void)tap2:(UITapGestureRecognizer *)tap
{
    self.tabBarController.tabBar.hidden = YES;
    GMHSecondViewController *detail = [[GMHSecondViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
    GMHActivity *act = self.blockOnearr[1];
    detail.url = act.url;
    
}

- (void)tap3:(UITapGestureRecognizer *)tap
{
    self.tabBarController.tabBar.hidden = YES;
    GMHSecondViewController *detail = [[GMHSecondViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
    GMHActivity *act = self.blockOnearr[2];
    detail.url = act.url;
    
}

- (void)taps:(UITapGestureRecognizer *)tap
{
    self.tabBarController.tabBar.hidden = YES;
    GMHSecondViewController *detail = [[GMHSecondViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
    GMHActivity *act = self.imagesArr[self.flag];
    detail.url = act.url;
}
//定时器
- (void)clip
{
    [self.scrollerView setContentOffset:CGPointMake(self.scrollerView.contentOffset.x + WIDTH, 0)];
//    if (self.scrollerView.contentOffset.x == self.imagesArr.count * WIDTH) {
//        self.scrollerView.contentOffset = CGPointMake(0, 0);
//    }
    if (self.scrollerView.contentOffset.x == (self.imagesArr.count + 1)* WIDTH) {
        self.scrollerView.contentOffset = CGPointMake(WIDTH, 0);
    }
    if (self.scrollerView.contentOffset.x == 0) {
        self.scrollerView.contentOffset = CGPointMake(self.imagesArr.count * WIDTH, 0);
    }

    self.page.currentPage = self.scrollerView.contentOffset.x / WIDTH - 1;
    self.flag = self.scrollerView.contentOffset.x / WIDTH;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.scrollerView.contentOffset.x == (self.imagesArr.count + 1)* WIDTH) {
        self.scrollerView.contentOffset = CGPointMake(WIDTH, 0);
    }
    if (self.scrollerView.contentOffset.x == 0) {
        self.scrollerView.contentOffset = CGPointMake(self.imagesArr.count * WIDTH, 0);
    }
    self.page.currentPage = scrollView.contentOffset.x / WIDTH - 1;
    self.flag = self.page.currentPage;
}

#pragma mark 选择城市按钮
//- (void)citys:(UIButton *)button
//{
//    self.tabBarController.tabBar.hidden = YES;
//    GMCitys *citys = [[GMCitys alloc]init];
//    [self.navigationController pushViewController:citys animated:YES];
//    [citys release];
//    
//    void (^block)(NSString *) = ^(NSString *str)
//    {
//        self.buttons.titleLabel.text = str;
//    };
//    citys.block = block;
//
//}

//判断是否有网络
-(BOOL) isConnectionAvailable{
    
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            NSLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            NSLog(@"WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            NSLog(@"3G");
            break;
    }
    
    if (!isExistenceNetwork) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];//<span style="font-family: Arial, Helvetica, sans-serif;">MBProgressHUD为第三方库，不需要可以省略或使用AlertView</span>
        hud.removeFromSuperViewOnHide =YES;
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"当前网络不可用，请检查网络连接";  //提示的内容
        hud.minSize = CGSizeMake(132.f, 58.0f);
        [hud hide:YES afterDelay:3];
        return NO;
    }
    
    return isExistenceNetwork;
}

//// 连接改变
//-(void)reachabilityChangeds:(NSNotification *)note
//{
//    Reachability *currReach = [note object];
//    NSParameterAssert([currReach isKindOfClass:[Reachability class]]);
//    
//    //对连接改变做出响应处理动作
//    NetworkStatus status = [currReach currentReachabilityStatus];
//    //如果没有连接到网络就弹出提醒实况
//    [GMH_Share SingleShare].isReachable = YES;
//    if(status == NotReachable)
//    {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接异常" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//        //        [alert show];
//        [alert release];
//        [GMH_Share SingleShare].isReachable = NO;
//        return;
//    }
//    else if (status==ReachableViaWiFi||status==ReachableViaWWAN) {
//        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接信息" message:@"网络连接正常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//        //        [alert show];
//        [alert release];
//        [GMH_Share SingleShare].isReachable = YES;
//    }
//}

@end
